package com.bb.blog.code.mybatis;

import com.bb.blog.code.mybatis.mapper.MapperXRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.io.IOException;

/**
 * 手写mybatis 1.0
 * 目标：
 * 1.实现 @Mapper 自动扫描并创建Bean
 * 2.实现 Mapper 与 statement 的映射
 */
@Configuration
public class StartConfiguration {
    private static final String CONFIG_LOCATION = "classpath*:/mapperX/*";
    private ResourcePatternResolver resourcePatternResolver;

    public StartConfiguration() {
        resourcePatternResolver = new PathMatchingResourcePatternResolver();
    }

    @Bean
    MapperXRegistry mapperXRegistry() {
        MapperXRegistry mapperXRegistry = new MapperXRegistry();
        try {

            Resource[] resources = resourcePatternResolver.getResources(CONFIG_LOCATION);
            MapperXRegistry.setMapperXLocation(resources);
            MapperXRegistry.parseMapperx();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return mapperXRegistry;
    }


}
